package com.ayoka.bean;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.jcp.baselibrary.utils.SPUtil;

/**
 * Created by jcp on 2018/1/3.
 */

public class UserInformation {

    private static UserInformation instance;

    public static UserInformation getInstance() {
        if (instance == null) {
            synchronized (UserInformation.class) {
                if (instance == null) {
                    instance = getUserInformation();
                }
            }
        }
        return instance;
    }

    // 获取用户
    private static UserInformation getUserInformation() {
        UserInformation user = null;
        String account = SPUtil.getString("USER_INFORMATION", "");
        if (!TextUtils.isEmpty(account)) {
            user = new Gson().fromJson(account, UserInformation.class);
        }
        return user;
    }

    // 设置用户
    public static synchronized void setUserInformation(UserInformation user) {
        instance = user;
        if (user != null) {
            String account = new Gson().toJson(user);
            SPUtil.putString("USER_INFORMATION", account);
        } else {
            SPUtil.removeSharedKey("USER_INFORMATION");
        }
    }

    // 设置用户
    public static void save() {
        if (instance != null) {
            setUserInformation(instance);
        }
    }

    /**
     * session_token : uiouiouio
     * user_info : {"ID":54,"UnionID":"oGsmB004HSuYPeXi2HFWILutqaCg","OpenID":"otR8H0UNVE4DkLTIl7ja6isClLas","XcxOpenID":"otR8H0UNVE4DkLTIl7ja6isClLas","AppOpenID":"otR8H0UNVE4DkLTIl7ja6isClLas","WeixinNickName":"amp程序猿@阿尤卡","Avatar":"https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIiaj20Gm91ialAz4E8KhKeFn7sl4t0Hg22jwO3QDeTMe3YEqVndHcvjcDicZehdThu06clUfUNH4I9w/0","LocalWxAvatar":"http://localhost:8848/upload/user_avatar/201712/6fe5f2a6-fdf2-4cc3-9307-eb4308ab63b9.jpg","Sex":1,"WeixinCity":"黄浦","WeixinCountry":"中国","WeixinProvince":"上海","TaskLevel":1,"TaskDay":1}
     */

    public String session_token;
    public String refresh_token;
    public String tim_identifier;
    public String tim_user_sig;
    public String tim_sig_expire;
    public int tim_sdk_appid;
    public int tim_sdk_account_type;
    public long logonTimeOut;
    public UserInfoBean user_info;

    public boolean hasMobile() {
       return user_info != null && user_info.BindCellphone == 1;
    }
}
